#include <iostream>
#include <vector>
#include <string>
#include <unordered_set>
#include <algorithm>
#include <iomanip>

using namespace std;

class Solution {
public:
    vector<vector<int>> generateMatrix(int n) {
        vector<vector<int>> vt(n, vector<int>(n));

        int colMax = n - 1, colMin = 0;
        int rowMax = n - 1, rowMin = 0;
        int col = 0;
        int row = 0;
        int flag = 0; // 0 1 2 3
        for (int idx = 1; idx <= n * n; idx++) {
            vt[row][col] = idx;
            switch(flag) {
                case 0:
                    col++;
                    if (col >= colMax) {
                        flag = 1;
                        rowMin++;
                    }
                    break;
                case 1:
                    row++;
                    if (row >= rowMax) {
                        flag = 2;
                        colMax--;
                    }
                    break;
                case 2:
                    col--;
                    if (col <= colMin) {
                        flag = 3;
                        rowMax--;
                    }
                    break;
                case 3:
                    row--;
                    if (row <= rowMin) {
                        flag = 0;
                        colMin++;
                    }
                    break;
                default:
                    break;
            }
        }
        return vt;
    }
};

int main()
{
    Solution s;
    vector<vector<int>> vt = s.generateMatrix(10);
    for (auto arr : vt) {
        for (auto elem : arr) {
            cout << right << setw(8) << elem << " ";
        }
        cout << endl;
    }
    return 0;
}